﻿<Window x:Class="ScrumSprintMonitor.ConfigEditor.Views.SprintConfigurationEditorView.SprintConfigurationEditorWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ScrumSprintMonitor.ConfigEditor"
    xmlns:WPFToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    xmlns:Progress="clr-namespace:Progress;assembly=ScrumSprintMonitor.UI.Wpf"
    xmlns:SprintConfigurationEditorView="clr-namespace:ScrumSprintMonitor.ConfigEditor.Views.SprintConfigurationEditorView"
    Background="Gray"
    Title="Sprint Configuration"
    WindowStartupLocation="CenterOwner" ShowInTaskbar="False" Closed="Window_Closed" Width="600" Loaded="Window_Loaded" MouseLeftButtonDown="Window_MouseLeftButtonDown">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ScrumSprintMonitor.UI.Wpf;component/Styles/FormResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <local:XamlServiceLocator x:Key="serviceLocator" />

            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>

            <Style x:Key="BaseDatePickerStyle" TargetType="{x:Type WPFToolkit:DatePicker}" BasedOn="{StaticResource {x:Type WPFToolkit:DatePicker}}">
                <Setter Property="Margin" Value="3"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
                <Setter Property="IsTodayHighlighted" Value="True"/>
            </Style>

            <Style x:Key="BaseTimePickerStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBoxStyle}">
                <Setter Property="Margin" Value="3"/>
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
            </Style>

            <Style x:Key="AdvanceSprintButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
                <Setter Property="Margin" Value="0,3"/>
                <Setter Property="Padding" Value="6"/>
            </Style>

            <!-- 
The template which renders a TextBox 
when it contains invalid data. 
-->
            <ControlTemplate x:Key="TextBoxErrorTemplate">
                <DockPanel>
                    <Ellipse DockPanel.Dock="Right" Margin="2,0" ToolTip="Contains invalid data" Width="10" Height="10">
                        <Ellipse.Fill>
                            <LinearGradientBrush>
                                <GradientStop Color="#11FF1111" Offset="0" />
                                <GradientStop Color="#FFFF0000" Offset="1" />
                            </LinearGradientBrush>
                        </Ellipse.Fill>
                    </Ellipse>
                    <!-- 
    This placeholder occupies where the TextBox will appear. 
    -->
                    <AdornedElementPlaceholder />
                </DockPanel>
            </ControlTemplate>

            <!-- 
The Style applied to both TextBox controls in the UI.
-->
            <Style TargetType="TextBox">
                <Setter Property="Margin" Value="4,4,10,4" />
                <Setter Property="Validation.ErrorTemplate" Value="{StaticResource TextBoxErrorTemplate}" />
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="True">
                        <Setter Property="ToolTip">
                            <Setter.Value>
                                <Binding Path="(Validation.Errors)[0].ErrorContent" RelativeSource="{x:Static RelativeSource.Self}"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ResourceDictionary>
    </Window.Resources>

    <Window.Style>
        <StaticResource ResourceKey="DefaultChrome"/>
    </Window.Style>

    <DockPanel Grid.IsSharedSizeScope="True" DataContext="{Binding Path=SprintConfigurationEditorViewModel, Source={StaticResource serviceLocator}}">
        <Grid DockPanel.Dock="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="PropertyName" />
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <ContentControl Grid.Row="0" Grid.ColumnSpan="2" ContentTemplate="{StaticResource Separator}" Content="Project" IsTabStop="False" Focusable="False" />

            <StackPanel Grid.Column="0" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="3">
                <Grid Width="18" Height="18">
                    <Viewbox Visibility="{Binding FetchingAvailableTfsProjects, Converter={StaticResource BooleanToVisibilityConverter}}">
                        <Progress:CircularProgressBar/>
                    </Viewbox>
                </Grid>
                <Label Margin="3,0,0,0" Style="{StaticResource LabelStyle}" Content="_Project Name:" Target="{Binding ElementName=tfsProjectNameComboBox}" />
            </StackPanel>
            <ComboBox Grid.Column="1" Grid.Row="1" Style="{StaticResource BaseComboBoxStyle}" ItemsSource="{Binding Path=AvailableTfsProjects}" SelectedItem="{Binding Path=TfsProjectName}" x:Name="tfsProjectNameComboBox" />

            <Label Style="{StaticResource LabelStyle}" Grid.Column="0" Grid.Row="2" Margin="3" Content="Process _Template:" Target="{Binding ElementName=processTemplateComboBox}" />
            <ComboBox x:Name="processTemplateComboBox" Grid.Column="1" Grid.Row="2" Style="{StaticResource BaseComboBoxStyle}" ItemsSource="{Binding Path=AvailableTfsProcessTemplates}" SelectedItem="{Binding Path=TfsProcessTemplateName}" />

            <ContentControl Grid.Row="3" Grid.ColumnSpan="2" ContentTemplate="{StaticResource Separator}" Content="Current Sprint" IsTabStop="False" Focusable="False" />

            <StackPanel Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="3">
                <Grid Width="18" Height="18">
                    <Viewbox Visibility="{Binding FetchingIterationPaths, Converter={StaticResource BooleanToVisibilityConverter}}">
                        <Progress:CircularProgressBar/>
                    </Viewbox>
                </Grid>
                <Label Margin="3,0,0,0" Style="{StaticResource LabelStyle}" Content="_Iteration Path:" Target="{Binding ElementName=iterationPathComboBox}" />
            </StackPanel>
            <ComboBox x:Name="iterationPathComboBox" Grid.Column="1" Grid.Row="4" Style="{StaticResource BaseComboBoxStyle}" ItemsSource="{Binding Path=IterationPaths}" SelectedItem="{Binding Path=CurrentIterationPath}" />

            <Grid Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="PropertyName"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <Label Style="{StaticResource LabelStyle}" Grid.Column="0" Grid.Row="0" Margin="3" Content="Sprint _Start Date:" Target="{Binding ElementName=sprintStartDatePicker}" />
                <Button Command="{Binding AdvanceSprintCommand}" CommandParameter="-1" Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Style="{StaticResource AdvanceSprintButtonStyle}" Content="&lt;"/>
                <Grid Grid.Column="2" Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="70" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <WPFToolkit:DatePicker x:Name="sprintStartDatePicker" Style="{StaticResource BaseDatePickerStyle}" SelectedDate="{Binding Path=SprintStartDate, Mode=TwoWay}" />
                    <TextBox Grid.Column="1" Style="{StaticResource BaseTimePickerStyle}" Text="{Binding Path=SprintStartTimeOfDay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"/>
                    <ComboBox Grid.Column="2" Style="{StaticResource BaseComboBoxStyle}" ItemsSource="{Binding Path=TimeZones}" SelectedItem="{Binding Path=SprintStartTimeZone}" />
                </Grid>

                <Label Style="{StaticResource LabelStyle}" Grid.Column="0" Grid.Row="1" Margin="3" Content="Sprint _End Date:" Target="{Binding ElementName=sprintEndDatePicker}" />
                <Grid Grid.Column="2" Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="70" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <WPFToolkit:DatePicker x:Name="sprintEndDatePicker" Style="{StaticResource BaseDatePickerStyle}" SelectedDate="{Binding Path=SprintEndDate, Mode=TwoWay}" />
                    <TextBox Grid.Column="1" Style="{StaticResource BaseTimePickerStyle}" Text="{Binding Path=SprintEndTimeOfDay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}" />
                    <ComboBox Grid.Column="2" Style="{StaticResource BaseComboBoxStyle}" ItemsSource="{Binding Path=TimeZones}" SelectedItem="{Binding Path=SprintEndTimeZone}" />
                </Grid>
                <Button Command="{Binding AdvanceSprintCommand}" CommandParameter="1" Grid.Column="3" Grid.Row="0" Grid.RowSpan="2" Style="{StaticResource AdvanceSprintButtonStyle}" Content="&gt;"/>
            </Grid>

            <ContentControl Grid.Row="7" Grid.ColumnSpan="4" ContentTemplate="{StaticResource Separator}" Content="Continuous Integration" IsTabStop="False" Focusable="False" />

            <StackPanel Grid.Column="0" Grid.Row="8" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="3">
                <Grid Width="18" Height="18">
                    <Viewbox Visibility="{Binding FetchingBuildDefinitions, Converter={StaticResource BooleanToVisibilityConverter}}">
                        <Progress:CircularProgressBar/>
                    </Viewbox>
                </Grid>
                <Label Margin="3,0,0,0" Style="{StaticResource LabelStyle}" Content="_Build Definition:" Target="{Binding ElementName=buildDefinitionListBox}" />
            </StackPanel>
            <ListBox x:Name="buildDefinitionListBox" Grid.Column="1" Grid.Row="8" Style="{StaticResource BaseListBoxStyle}" MinHeight="24" ItemsSource="{Binding Path=BuildDefinitionStates}" MaxHeight="200" Focusable="False">
                <ListBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type SprintConfigurationEditorView:BuildDefinitionItemViewModel}">
                        <CheckBox IsChecked="{Binding IsEnabled}">
                            <TextBlock Padding="3" Foreground="White" Text="{Binding Name}"/>
                        </CheckBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

        <StackPanel DockPanel.Dock="Bottom" HorizontalAlignment="Right" Orientation="Horizontal" VerticalAlignment="Bottom" Margin="0,12,0,0">
            <Button Command="{Binding OkCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Margin="6,0,0,0" Padding="16,4" IsDefault="True" Content="O_K"/>
            <Button Command="{Binding CancelCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" x:Name="cancelButton" Margin="6,0,0,0" Padding="16,4" IsCancel="True" Content="Cancel"/>
        </StackPanel>
    </DockPanel>
</Window>
